package com.hsgene.product.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.product.condition.CommentCondition;
import com.hsgene.product.domain.GeneticCommentReply;
import com.hsgene.product.dto.GeneticCommentDto;
import com.hsgene.product.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 商品评论
 * @projectName: hdas-geneshop
 * @Package: com.hsgene.product.controller
 * @Author: FX
 * @CreateDate: 2018/10/19 9:58
 * @Version: 1.0
 * Copyright: Copyright (c) 2018
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加回复
     *
     * @param geneticCommentReply
     * @return
     * @author FX
     * @date 2018/10/19 11:20
     */
    @PostMapping
    public ApiResult addGeneticCommentReply(@RequestBody GeneticCommentReply geneticCommentReply) {
        return commentService.addGeneticCommentReply(geneticCommentReply);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     * @author FX
     * @date 2018/10/19 13:35
     */
    @GetMapping("{id}")
    public ApiResult<GeneticCommentDto> getCommentById(@PathVariable("id") String id) {
        return commentService.getCommentById(id);
    }

    /***
     * 商品评价列表
     * @author jcl
     * @date 2018/10/19 15:03
     * @param condition 查询条件
     * @param page 页码
     * @param size 大小
     * @return
     */
    @GetMapping
    public ApiResult comments(CommentCondition condition, Integer page, Integer size) {
        try {
            if (page == null || size == null) {
                return new ApiResult(5001, "failed", "分页条件不能为空", null);
            }
            LimitHelper limitHelper = LimitHelper.create(page, size, true);
            condition.setLimitHelper(limitHelper);
            return commentService.comments(condition);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(500, "failed", "获取数据失败", e.toString());
        }
    }
}
